package practise.alogrithm.gotoSiliconValley.array;

import java.util.HashMap;
import java.util.Map;

/**
 * find out the all the two elements combinations equal to the target.use O(n) as the extra storage, find out any one is OK
 * */
public class SumOfTwoNumberII {
    public int[] hasSum(int[] arrays, int target){
        if(arrays==null || arrays.length<2){
            return null;
        }
        int length = arrays.length;
        int[] result = {-1,-1};
        Map<Integer, Integer> valueIndexMapper = new HashMap<>();
        for(int index=0; index<arrays.length   ; index++){
            valueIndexMapper.put(arrays[index],index);
        }
        for(Integer value : valueIndexMapper.keySet()){
            if(value*2==target){
                continue;
            }
            if(valueIndexMapper.containsKey(target-value)){
                result[0]=valueIndexMapper.get(value);
                result[1]=valueIndexMapper.get(target-value);
                break;//return the first value
            }
        }
        return result;
    }
}
